﻿using System;
using Monkey.Framework.Logging;
using Monkey.Warrenty.Data;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Biz
{
    public class ServiceBiz
    {
        #region Properties

        private readonly IServiceDao _serviceDao;

        #endregion

        #region Constructors

        public ServiceBiz()
        {
            _serviceDao = new ServiceDao();
        }

        public ServiceBiz(IServiceDao serviceDao)
        {
            _serviceDao = serviceDao;
        }

        #endregion

        #region Methods

        public Service GetById(int id)
        {
            if (id <= 0) return null;
            try
            {
                return _serviceDao.GetById(id);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }

            return null;
        }

        public Service CreateService(Service service)
        {
            // validate inputs
            if (service == null) return null;
            if (string.IsNullOrEmpty(service.Code) || string.IsNullOrEmpty(service.Name)) return null;

            try
            {
                return _serviceDao.AddService(service);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public int GetMaxId()
        {
            return _serviceDao.GetMaxId();
        }

        public int GetNextCode()
        {
            return _serviceDao.GetCurrentIdentity() + 1;
        }

        public void UpdateService(Service service)
        {
            if (service == null) return;
            try
            {
                _serviceDao.UpdateService(service);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }
        }

        public object GetServices(string name, int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                var count = _serviceDao.GetServiceCount(name);
                var services = _serviceDao.GetServices(name, jtStartIndex, jtPageSize, jtSorting);
                return new { Result = "OK", Records = services, TotalRecordCount = count };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object CreateServiceWs(Service service)
        {
            // validate inputs
            if (service == null) return null;
            if (string.IsNullOrEmpty(service.Code) || string.IsNullOrEmpty(service.Name)) return null;

            try
            {
                var addedService = _serviceDao.AddService(service);
                return new { Result = "OK", Record = addedService };
            }
            catch (Exception ex)
            {
                //throw;
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object UpdateServiceWs(Service service)
        {
            if (service == null) return null;
            try
            {
                _serviceDao.UpdateService(service);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object DeleteServiceWs(int serviceId)
        {
            try
            {
                _serviceDao.DeleteService(serviceId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        #endregion

        #region Helpers

        #endregion
    }
}
